package com.qianbao.service.money.impl;

import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.excepion.ExceptionEnum;
import com.qianbao.components.uuid.SVUuidTool;
import com.qianbao.dao.money.CoinDao;
import com.qianbao.dao.money.WidthdrawDao;
import com.qianbao.domain.widthdraw.WidthdrawEntity;
import com.qianbao.service.money.WidthdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
public class WidthdrawServiceImpl implements WidthdrawService {

    @Autowired
    private WidthdrawDao widthdrawDao;

    @Autowired
    private CoinDao coinDao;


    @Override
    @Transactional
    public void saveUserWidthdrawInformation(WidthdrawEntity entity) throws CommonException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String currentTime = format.format(new Date());

        entity.setCreate_time(currentTime);
        entity.setWidthdraw_id(SVUuidTool.getUUID());

        int rows = widthdrawDao.saveUserWidthdrawInfomation(entity);

        int coinNumber = - Integer.parseInt(entity.getWidthdraw_money()) * 10000;
        int rows1 = coinDao.saveUserCoin(entity.getCustom_id(), coinNumber);

        if (rows <= 0){
            throw new CommonException(ExceptionEnum.Sql_insert_fail.getCode(), ExceptionEnum.Sql_insert_fail.getMessage());
        }

        if (rows1 <= 0){
            throw new CommonException(ExceptionEnum.Sql_update_fail.getCode(), ExceptionEnum.Sql_update_fail.getMessage());
        }

    }

    @Override
    public List<WidthdrawEntity> findUserWidthdrawRecord(String custom_id) throws CommonException {

        if (custom_id == null || custom_id.length() <= 0){
            throw new CommonException(ExceptionEnum.Request_Params_Invalid.getCode(),
                    ExceptionEnum.Request_Params_Invalid.getMessage());
        }

        List<WidthdrawEntity> entityList = widthdrawDao.findUserWidthdrawInfomation(custom_id);
        return entityList;
    }


}





















